<?php

namespace app\api\controller\classes;

use app\common\controller\Api;
use app\common\model\User;
use think\Db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Classes extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * Classes模型对象
     * @var \app\admin\model\classes\Classes
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classes\Classes;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
//        return 123;
//        //当前是否为关联查询
//        $this->relationSearch = true;
//        //设置过滤方法
//        $this->request->filter(['strip_tags', 'trim']);
//        if ($this->request->isAjax()) {
        //如果发送的来源是Selectpage，则转发到Selectpage
//            if ($this->request->request('keyField')) {
//                return $this->selectpage();
//            }
//            $adming_where=[];
//            if (!$this->auth->isSuperAdmin())
//            {
//                $adming_where['fa_classes_class.uid'] = get_admin_id($this->auth->id);
//            }
//            if(get_auth_group($this->auth->id) == 3)
//            {
//                $profile_id = get_profile_id($this->auth->id);
//                $adming_where['fa_classes_class.profile_id'] = $profile_id;
//            }
//            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//            dump($this->request->get());die;
        $is_end_course = $this->request->post('is_end_course/d', 0);
        $userinfo = $this->auth->getUserinfo();
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');
        $where ['fa_classes_teacher.profile_id'] = $profile_id;
        $where['is_end_course'] = $is_end_course;

        $sort = 'id';
        $order = 'DESC';
        $offset = 1;
        $limit = 10;
        $list = $this->model
            ->with(['teacher'])
            ->where($where)
//                    ->where($adming_where)
            ->order($sort, $order)
            ->paginate($limit);
//            echo $this->model->getLastSql();die;
        foreach ($list as $row) {
            $row->visible(['id', 'title', 'disable', 'status', 'grade', 'entry_year', 'profile_id', 'requireVerify', 'realname', 'mobile', 'is_end_course']);
            $row->visible(['teacher']);
            $row->getRelation('teacher')->visible(['realname', 'mobile']);

        }
        $lists=[];
//        return $this->success($list);
        foreach ( $list->items() as $key=>$item){
            $lists[$key]['id']=$item->id;
            $lists[$key]['title']=$item->title;
            $lists[$key]['entry_year']=$item->entry_year;
            $lists[$key]['profile_id']=$item->profile_id;
            $lists[$key]['requireVerify']=$item->requireVerify;
            $lists[$key]['disable']=$item->disable;
            $lists[$key]['is_end_course']=$item->is_end_course;
            $lists[$key]['studentcount']=User::where(['classs'=>$item->id])->count();
            $lists[$key]['teacher']['realname']=$item->teacher->realname;
            $lists[$key]['teacher']['mobile']=$item->teacher->mobile;
//            return $this->success($item);
        }
        $result = array("total" => $list->total(), "rows" => $lists);

        return json($result);
//        }
//        return $this->view->fetch();
    }

    public function endClass()
    {
        $class_id = $this->request->post('class_id/d', 0);
        if (empty($class_id)) {
            return $this->error('缺少重要参数');
        }
        $ClassessModel = new \app\admin\model\classes\Classes();
        $userinfo = $this->auth->getUserinfo();
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');
        if (null == $Classess = $ClassessModel->find($class_id)) {
            return $this->error('班级不存在#1');
        }
        if ($Classess->profile_id != $profile_id) {
            return $this->error('班级不存在#2');
        }
        if ($Classess->is_end_course == 1) {
            return $this->error('该课程已结束');
        }
        if(strtotime($Classess->end_time." 23:59:59")<time()){
            return $this->error('该课程已结束');
        }
        $ClassessModel->where(['id'=>$Classess->id])->update(['is_end_course'=>1]);
        return $this->success('结课成功');
//        $Classess->is_end_course==1
//        $Classess->save();
    }

//    public function add()
//    {
//        if ($this->request->isPost())
//        {
//            $params = $this->request->post("row/a");
//
//            Db::startTrans();
//            try {
//                if(empty($params['profile_id']))
//                {
//                    $tlist = Db::name('classes_teacher')->where(['user_id'=>$this->auth->id])->find();
//                    $params['profile_id'] = $tlist['profile_id'];
//                }
//                $params['uid'] = get_admin_id($this->auth->id);
//                Db::name('classes_class')->insert($params);
//
//                Db::commit();
//            } catch (\Exception $e) {
//                Db::rollback();
//                $this->error($e->getMessage());
//            }
//            $this->success();
//        }
//        $agalist = Db::name('auth_group_access')->where('uid',$this->auth->id)->find();
//        $is_show = 0;
//        if($agalist['group_id'] == 1 || $agalist['group_id'] == 2)
//        {
//            $is_show = 1;
//        }
//        $this->view->assign('is_show',$is_show);
//        $where = [];
//        if($this->auth->id != 1)
//        {
//            $where = ['admin_id'=>$this->auth->id];
//        }
//        $tlist = Db::name('classes_teacher')->where($where)->select();
//        $this->view->assign('tlist',$tlist);
//        return $this->view->fetch();
//    }
//    public function edit($ids = null)
//    {
//        $row = Db::name('classes_class')->find($ids);
//        if ($this->request->isPost())
//        {
//            $params = $this->request->post("row/a");
//
//            Db::startTrans();
//            try {
//
//                Db::name('classes_class')->where('id',$row['id'])->update($params);
//
//                Db::commit();
//            } catch (\Exception $e) {
//                Db::rollback();
//                $this->error($e->getMessage());
//            }
//            $this->success();
//        }
//        $this->view->assign('row',$row);
//        return $this->view->fetch();
//    }

}
